package com.app.hmst.main;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import ru.bartwell.exfilepicker.ExFilePicker;
import ru.bartwell.exfilepicker.ExFilePickerParcelObject;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.os.StrictMode;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.util.Base64;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.app.hmst.model.CategoryVO;
import com.app.hmst.model.HomestayInfoVO;
import com.app.hmst.model.HomestayVO;
import com.app.hmst.model.HouseInfoVO;
import com.app.hmst.model.IndicatorResultVO;
import com.app.hmst.model.IndicatorVO;
import com.app.hmst.model.SubCategoryVO;
import com.app.hmst.utils.GlobalVariable;
import com.app.hmst.utils.TransparentPanel;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.roscopeco.ormdroid.Entity;


@SuppressLint({ "NewApi", "ValidFragment" }) public class EvaluateFormFragment extends Fragment{
	private View v;
	private SlidingMenu _sm;
	private ImageView menu_icon,yesBt,noBt,noneBt,interView,documentView,viewImage,saveEvaluateValues,clickToManageImage,captureImage,selectFromSD,clickToManageComments,clickToDrawing;
	private ImageView prevInd,nextInd,submitBt,clickToNoti,clickToEnterText;
	private RelativeLayout clickToPrevious,formPage;
	private HomestayInfoVO homestayInfoVO;
	private TextView homestayName,homestayProvince,catCountStr,catRatio,catTitle,subCountStr,subTotal,subName,indCountStr,indTotal,indName,noOfListColumn2,noOfListColumn3,noOfListColumn1;
	private HomestayVO homestayVO;
	private int catCount=0,selCatCount=0;
	private int subCount=0,selSubCount=0;
	private int indCount=0,selIndCount=0;
	private CategoryVO categoryVO,selCategoryVO;
	private SubCategoryVO subCategoryVO,selSubCategoryVO;
	private IndicatorVO indicatorVO;
	private boolean isHomeStay;
	private ListView listViewForFirstSelect,listViewForSecondSelect,listViewForThirdSelect;
	private HouseInfoVO houseInfoVO;
	private GridView preImgGridView;
	private EditText addingComment;
	public Animation animShow, animHide;
	public FloatingActionButton hideButton;
	public TransparentPanel popup,popupForComment;
	private int columnWidth;
	private Utils utils;
	private static final String IMAGE_DIRECTORY_NAME = "SURVEY_ACESS";
	private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
	public static final int MEDIA_TYPE_IMAGE = 1;
	public static final int MEDIA_TYPE_VIDEO = 2;
	private Uri fileUri; // file url to store image/video
	private List<String> listImgPath;
	private static final int EX_FILE_PICKER_RESULT = 0;
	private FloatingActionButton clickToHideComments;
	private ListViewFirstInitMenuAdapter lv1adapter;
	private int trigger = 0;
	private boolean isComment = false;
	private IndicatorResultVO inResultVO;
	private static AlertDialog.Builder builder1;
	public EvaluateFormFragment(SlidingMenu sm,HomestayInfoVO homestayInfoVO,boolean isHomeStay){
		this._sm = sm;
		this.homestayInfoVO = homestayInfoVO;
		this.isHomeStay = isHomeStay;
	}
	public EvaluateFormFragment(SlidingMenu sm,HomestayInfoVO homestayInfoVO,boolean isHomeStay,int catCount){
		this._sm = sm;
		this.homestayInfoVO = homestayInfoVO;
		this.isHomeStay = isHomeStay;
		this.catCount = catCount;
	}
	public EvaluateFormFragment(SlidingMenu sm,HomestayInfoVO homestayInfoVO,HouseInfoVO houseInfoVO,boolean isHomeStay){
		this._sm = sm;
		this.homestayInfoVO = homestayInfoVO;
		this.isHomeStay = isHomeStay;
		this.houseInfoVO = houseInfoVO;
	}
	public EvaluateFormFragment(SlidingMenu sm,HomestayInfoVO homestayInfoVO,HouseInfoVO houseInfoVO,boolean isHomeStay,int catCount){
		this._sm = sm;
		this.homestayInfoVO = homestayInfoVO;
		this.isHomeStay = isHomeStay;
		this.houseInfoVO = houseInfoVO;
		this.catCount = catCount;
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
		System.gc();
		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
		StrictMode.setThreadPolicy(policy);
		
		v = inflater.inflate(R.layout.fragment_evaluate_page,container, false);
		
		listViewForThirdSelect = (ListView) v.findViewById(R.id.listViewThirdSelect);
		listViewForThirdSelect.setDivider(null);
		listViewForSecondSelect = (ListView) v.findViewById(R.id.listViewSecondSelect);
		listViewForSecondSelect.setDivider(null);
		listViewForFirstSelect = (ListView) v.findViewById(R.id.listViewForFirstSelect);
		listViewForFirstSelect.setDivider(null);
		noOfListColumn2 = (TextView) v.findViewById(R.id.noOfListColumn2);
		noOfListColumn3 = (TextView) v.findViewById(R.id.noOfListColumn3);
		noOfListColumn1 = (TextView) v.findViewById(R.id.noOfListColumn1);
		saveEvaluateValues = (ImageView) v.findViewById(R.id.imageView18);
		formPage = (RelativeLayout) v.findViewById(R.id.relativeLayout4);
		clickToEnterText = (ImageView) v.findViewById(R.id.imageView19);
		catCountStr = (TextView) v.findViewById(R.id.textView7);
		catRatio = (TextView) v.findViewById(R.id.textView8);
		catTitle = (TextView) v.findViewById(R.id.textView9);
		subCountStr = (TextView) v.findViewById(R.id.textView11);
		subTotal = (TextView) v.findViewById(R.id.textView12);
		subName = (TextView) v.findViewById(R.id.textView13);
		indCountStr = (TextView) v.findViewById(R.id.textView16);
		indTotal = (TextView) v.findViewById(R.id.textView17);
		indName = (TextView) v.findViewById(R.id.textView19);
		yesBt = (ImageView) v.findViewById(R.id.imageView10);
		noBt = (ImageView) v.findViewById(R.id.imageView11);
		noneBt = (ImageView) v.findViewById(R.id.imageView12);
		interView = (ImageView) v.findViewById(R.id.imageView14);
		documentView = (ImageView) v.findViewById(R.id.imageView13);
		viewImage = (ImageView) v.findViewById(R.id.imageView15);
		addingComment = (EditText) v.findViewById(R.id.editText1);
		prevInd = (ImageView) v.findViewById(R.id.imageView8);
		nextInd = (ImageView) v.findViewById(R.id.imageView7);
		submitBt =  (ImageView) v.findViewById(R.id.imageView21);
		
		clickToPrevious = (RelativeLayout) v.findViewById(R.id.relativeLayout2);
		clickToManageImage = (ImageView) v.findViewById(R.id.imageView16);			
		
		hideButton = (FloatingActionButton) v.findViewById(R.id.button_floating_action_for_close);
		popup = (TransparentPanel) v.findViewById(R.id.popup_window);
		popup.setVisibility(View.GONE);		
	
		
		clickToManageComments = (ImageView) v.findViewById(R.id.imageView17);
		popupForComment = (TransparentPanel) v.findViewById(R.id.popup_window_for_comment);
		popupForComment.setVisibility(View.GONE);
		initPopupForComment();
		clickToHideComments = (FloatingActionButton) v.findViewById(R.id.button_floating_action_for_comment);
		clickToNoti = (ImageView) v.findViewById(R.id.imageView9);
		clickToNoti.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				switchFragment(new ViewAllEvaluatePageFragment(_sm));
			}
		});
		clickToDrawing = (ImageView) v.findViewById(R.id.imageView20);
		clickToDrawing.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(getActivity(), CanvasMainActivity.class);
				if(isHomeStay){
					inResultVO = Entity.query(IndicatorResultVO.class).where("indicatorId").eq(indicatorVO.id+"").and("homeId").eq(homestayInfoVO.id).execute();
					}else{
					inResultVO = Entity.query(IndicatorResultVO.class).where("indicatorId").eq(indicatorVO.id+"").and("homeId").eq(homestayInfoVO.id)
								.and("houseId").eq(houseInfoVO.id).execute();	
					}
					
					if(inResultVO==null){
						inResultVO = new IndicatorResultVO();
						inResultVO.id = inResultVO.getPk();
						inResultVO.indicatorId = indicatorVO.id+"";
						inResultVO.categoryId = categoryVO.id+"";
						inResultVO.homeId = homestayInfoVO.id+"";
					}
					if(isHomeStay){
						inResultVO.houseId ="0";
					}else{
						inResultVO.houseId = houseInfoVO.id+"";
					}
				i.putExtra("inResultVO", inResultVO); 
				startActivity(i);
				addingComment.setVisibility(View.GONE);
			}
		});
		new Utils(getActivity()).onBackPressed(v, new GeneralInfoFragment(_sm,homestayInfoVO),null);
		//Manage Camera
		clickToEnterText.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				addingComment.setVisibility(View.VISIBLE);
				addingComment.setFocusableInTouchMode(true);
				if(inResultVO!=null){
				addingComment.setText(inResultVO.comment_text);
				}
			}
		});
		captureImage = (ImageView) v.findViewById(R.id.imageView1);
		preImgGridView = (GridView) v.findViewById(R.id.preImgGridView);
//		floatingActionButton = (FloatingActionButton) v.findViewById(R.id.button_floating_action);
		//floatingActionButton.attachToListView(preImgGridView);
		//floatingActionButton.setVisibility(View.INVISIBLE);
		selectFromSD = (ImageView) v.findViewById(R.id.imageView2);
		utils = new Utils(getActivity().getApplicationContext());
		InitilizeGridLayout();
		captureImage.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(listImgPath!=null && listImgPath.size()<3){
					captureImage();
				}else{
					uploadImageError();
				}
			}
		});
		selectFromSD.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				if(listImgPath!=null && listImgPath.size()<3){
				Intent intent = new Intent(getActivity().getApplicationContext(), ru.bartwell.exfilepicker.ExFilePickerActivity.class);
				intent.putExtra(ExFilePicker.SET_FILTER_LISTED, new String[] { "jpg", "jpeg" });
				startActivityForResult(intent, EX_FILE_PICKER_RESULT);
				}else{
					uploadImageError();
				}
			}
		});
	
		clickToHideComments.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				AlertDialog.Builder builder1 = new AlertDialog.Builder(getActivity());
		        builder1.setMessage(getString(R.string.quitwithoutsave_message));
		        builder1.setCancelable(true);
		        builder1.setPositiveButton(getString(R.string.homestay_sync_ok),new DialogInterface.OnClickListener() {
		            public void onClick(DialogInterface dialog, int id) {
		                dialog.cancel();
		                isComment = false;
						popupForComment.startAnimation(animHide);
						// showButton.setEnabled(true);
						clickToHideComments.setEnabled(true);
						popupForComment.setVisibility(View.GONE);
						addingComment.setVisibility(View.GONE);
						inResultVO.comment_text = addingComment.getText().toString();
						inResultVO.save();
		            }
		        });
		        builder1.setNegativeButton(getString(R.string.cancel_message), new DialogInterface.OnClickListener() {
		            public void onClick(DialogInterface dialog, int id) {
		                dialog.cancel();
		                isComment = false;
						popupForComment.startAnimation(animHide);
						// showButton.setEnabled(true);
						clickToHideComments.setEnabled(true);
						popupForComment.setVisibility(View.GONE);
						addingComment.setVisibility(View.GONE);
		               
		            }
		        });
		        AlertDialog alert11 = builder1.create();
		        alert11.show();
				
				
			}
		});
		clickToManageImage.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
			new InitAndLoadImage().execute();

			}
		});
		clickToManageComments.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(isHomeStay){
					inResultVO = Entity.query(IndicatorResultVO.class).where("indicatorId").eq(indicatorVO.id+"").and("homeId").eq(homestayInfoVO.id).execute();
					}else{
					inResultVO = Entity.query(IndicatorResultVO.class).where("indicatorId").eq(indicatorVO.id+"").and("homeId").eq(homestayInfoVO.id)
								.and("houseId").eq(houseInfoVO.id).execute();	
				}
				if(inResultVO!=null){
					addingComment.setText(inResultVO.comment_text);
				}else{
					addingComment.setText(null);
					
				}
				addingComment.setVisibility(View.VISIBLE);
				addingComment.setFocusableInTouchMode(true);
				addingComment.requestFocus(); 
				popupForComment.setVisibility(View.VISIBLE);
				popupForComment.startAnimation(animShow);
				isComment = true;
				popup.startAnimation(animHide);
				// showButton.setEnabled(true);
				hideButton.setEnabled(true);
				popup.setVisibility(View.GONE);
			}
		});
		//Execute background process
		//new InitAndLoadData().execute();
		homestayName = (TextView) v.findViewById(R.id.textView4);
		homestayName.setText(homestayInfoVO.name);
		homestayProvince = (TextView) v.findViewById(R.id.textView3);
		homestayProvince.setText(homestayInfoVO.province);
		if(isHomeStay){
		homestayVO = Entity.query(HomestayVO.class).where("type").eq(GlobalVariable.TYPE_HOMESTAY).execute();
		}else{
		homestayVO = Entity.query(HomestayVO.class).where("type").eq(GlobalVariable.TYPE_HOUSE).execute();
		}
		
		
		categoryVO =  homestayVO.categories().get(catCount);
		catCountStr.setText(categoryVO.no);
		catRatio.setText((catCount+1)+"/"+homestayVO.categories().size());
		catTitle.setText(homestayVO.categories().get(catCount).title);
		
		subCategoryVO = categoryVO.subCategories().get(subCount);
		subCountStr.setText((subCount+1)+"");
		subTotal.setText("/"+categoryVO.subCategories().size());
		subName.setText(subCategoryVO.title);
		
		indicatorVO = subCategoryVO.indicators().get(indCount);
		indCountStr.setText((indCount+1)+"");
		indTotal.setText("/"+subCategoryVO.indicators().size());
		indName.setText(indicatorVO.title);
		
		checkBt();
		
		initPopup();
		
		 
		new InitForListViewFirstSelect(0).execute();
		new InitForListViewFirstSelect(1,0).execute();
		new InitForListViewFirstSelect(2,0).execute();
		
		listViewForFirstSelect.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				onchangeCategory(arg0, arg1, arg2);
				new InitForListViewFirstSelect(1,arg2).execute();

			}

			
		});
	
		listViewForSecondSelect.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				onchangeSubCategory(arg0, arg1, arg2);
			}
		});
		listViewForThirdSelect.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				new fakeLoading().execute();
				catCount = selCatCount;
				subCount = selSubCount;
				indCount = arg2;
				View v;
			    int count = arg0.getChildCount();
			            for (int i=0; i<count; i++)
			            {
			                if (i!= arg2)
			                {
			                    v = arg0.getChildAt(i); 
			                    IndicatorVO tempIndVO =  selSubCategoryVO.indicators().get(i);
			                    IndicatorResultVO tempResult = null;
								if(isHomeStay){
									tempResult = Entity.query(IndicatorResultVO.class).where("indicatorId").eq(tempIndVO.id).and("homeId")
											.eq(homestayInfoVO.id).and("result").isNotNull().execute();
								}else{
									tempResult = Entity.query(IndicatorResultVO.class).where("indicatorId").eq(tempIndVO.id).and("homeId")
											.eq(homestayInfoVO.id).and("houseId").eq(houseInfoVO.id).and("result").isNotNull().execute();	
								}
								if(tempResult!=null){
									 RelativeLayout layout_list = (RelativeLayout) v.findViewById(R.id.layout_list);
									layout_list.setBackground(getResources().getDrawable(R.drawable.round_auto_select_layout));
								}else{
									RelativeLayout layout_list = (RelativeLayout) v.findViewById(R.id.layout_list);
								    layout_list.setBackground(getResources().getDrawable(R.drawable.round_default_layout));
								}

			                }
			            }

			    RelativeLayout layout_list = (RelativeLayout) arg1.findViewById(R.id.layout_list);
			    layout_list.setBackground(getResources().getDrawable(R.drawable.round_layout_green_select));
				updateSelForms();
			}
		});
		
		
		formPage.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				switchFragment(new FormNotePageFragment(_sm,homestayInfoVO));
			}
		});
//		subCountStr = (TextView) v.findViewById(R.id.textView11);
//		subTotal = (TextView) v.findViewById(R.id.textView12);
//		indCountStr = (TextView) v.findViewById(R.id.textView16);
//		indTotal = (TextView) v.findViewById(R.id.textView17);
//		indName = (TextView) v.findViewById(R.id.textView19);
		
		clickToPrevious.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				switchFragment(new GeneralInfoFragment(_sm,homestayInfoVO));
			}
		});
		menu_icon = (ImageView) v.findViewById(R.id.imageView6);
		menu_icon.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				_sm.toggle();
			}
		});
		yesBt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				saveForms("0");
				checkBt();
			}
		});
		noBt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				saveForms("1");
				checkBt();
			}
		});
		noneBt.setOnClickListener(new OnClickListener() {
	
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				saveForms("2");
				checkBt();
			}
		});
		nextInd.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				updateForms();
			}
		});
		prevInd.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				updatePrevForms();
			}
		});
		submitBt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(isHomeStay){
					switchFragment(new AfterSaveEvaluateFormFragment(_sm,homestayVO,homestayInfoVO,null,isHomeStay));
				}else{
					switchFragment(new AfterSaveEvaluateFormFragment(_sm,homestayVO,homestayInfoVO,houseInfoVO,isHomeStay));	
				}
				
			}
		});
		
		
		saveEvaluateValues.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				new fakeLoading().execute();
				
				if(isComment){
				popupForComment.startAnimation(animHide);
				// showButton.setEnabled(true);
				clickToHideComments.setEnabled(true);
				popupForComment.setVisibility(View.GONE);
				addingComment.setVisibility(View.GONE);
				isComment = false;
				}
				
				if(isHomeStay){
				inResultVO = Entity.query(IndicatorResultVO.class).where("indicatorId").eq(indicatorVO.id+"").and("homeId").eq(homestayInfoVO.id).execute();
				}else{
				inResultVO = Entity.query(IndicatorResultVO.class).where("indicatorId").eq(indicatorVO.id+"").and("homeId").eq(homestayInfoVO.id)
								.and("houseId").eq(houseInfoVO.id).execute();	
				}
				if(inResultVO==null){
					inResultVO = new IndicatorResultVO();
					inResultVO.id = inResultVO.getPk();
					inResultVO.indicatorId = indicatorVO.id+"";
					inResultVO.categoryId = categoryVO.id+"";
					inResultVO.homeId = homestayInfoVO.id+"";
				}
				if(isHomeStay){
					inResultVO.houseId ="0";
				}else{
					inResultVO.houseId = houseInfoVO.id+"";
				}
//				if(!"".equals(addingComment.getText().toString())){
//					inResultVO.comment_text = addingComment.getText().toString();
//				
//				}
				inResultVO.save();
				updateForms();
				
			}
		});
		
	
		return v;
	}
	private void switchFragment(Fragment fragment) {
		if (getActivity() == null)
			return;

		if (getActivity() instanceof HomeStayMainActivity) {
			HomeStayMainActivity fca = (HomeStayMainActivity) getActivity();
			fca.switchContent(fragment);
		}
	}
	public void updateForms(){
		if(catCount<homestayVO.categories().size()){
			if(((catCount+1)<homestayVO.categories().size()) || ((subCount+1)<categoryVO.subCategories().size()) 
					|| ((indCount+1) < subCategoryVO.indicators().size()))
			{
				if((indCount+1)<subCategoryVO.indicators().size()){
					indCount++;
						categoryVO =  homestayVO.categories().get(catCount);
						subCategoryVO = categoryVO.subCategories().get(subCount);
						indicatorVO = subCategoryVO.indicators().get(indCount);
						View indList = listViewForThirdSelect.getChildAt(indCount);
						if(indList!=null){
							View v;
						            for (int i=0; i<indCount; i++)
						            {
						                if (i!= indCount)
						                {
						                    v = listViewForThirdSelect.getChildAt(i);
						                    List<IndicatorResultVO> tempResult = null;
						                    IndicatorVO tempIndicatorVO = subCategoryVO.indicators().get(i);
											if(isHomeStay){
												tempResult = Entity.query(IndicatorResultVO.class).where("indicatorId").eq(tempIndicatorVO.id).and("homeId")
														.eq(homestayInfoVO.id).and("result").isNotNull().executeMulti();
											}else{
												tempResult = Entity.query(IndicatorResultVO.class).where("indicatorId").eq(tempIndicatorVO.id).and("homeId")
														.eq(homestayInfoVO.id).and("houseId").eq(houseInfoVO.id).and("result").isNotNull().executeMulti();	
											}
											if(tempResult!=null && !tempResult.isEmpty()){
												RelativeLayout layout_list2 = (RelativeLayout) v.findViewById(R.id.layout_list);
												layout_list2.setBackground(getResources().getDrawable(R.drawable.round_auto_select_layout));
											}else{
												RelativeLayout layout_list2 = (RelativeLayout) v.findViewById(R.id.layout_list);
												layout_list2.setBackground(getResources().getDrawable(R.drawable.round_default_layout));
											}

						                }
						            }

						    RelativeLayout layout_list = (RelativeLayout) indList.findViewById(R.id.layout_list);
							layout_list.setBackground(getResources().getDrawable(R.drawable.round_select_layout));
						}
				}else{
					indCount = 0;
					subCount++;
					if(subCount<categoryVO.subCategories().size()){
						categoryVO =  homestayVO.categories().get(catCount);
						subCategoryVO = categoryVO.subCategories().get(subCount);
						indicatorVO = subCategoryVO.indicators().get(indCount);
						View subList = listViewForSecondSelect.getChildAt(subCount);
						if(subList!=null){
							View v;
						            for (int i=0; i<subCount; i++)
						            {
						                if (i!= subCount)
						                {
						                    v = listViewForSecondSelect.getChildAt(i); 
						                    SubCategoryVO tempSubCategoryVO =  selCategoryVO.subCategories().get(i);
								            List<IndicatorResultVO> tempResult = null;
											if(isHomeStay){
												tempResult = Entity.query(IndicatorResultVO.class).where("subCategoryId").eq(tempSubCategoryVO.id).and("homeId")
														.eq(homestayInfoVO.id).and("result").isNotNull().executeMulti();
											}else{
												tempResult = Entity.query(IndicatorResultVO.class).where("subCategoryId").eq(tempSubCategoryVO.id).and("homeId").eq(homestayInfoVO.id)
															.and("houseId").eq(houseInfoVO.id).and("result").isNotNull().executeMulti();	
											}
											if(tempResult!=null){
												if(tempSubCategoryVO.indicators().size()==tempResult.size()){
													RelativeLayout layout_list3 = (RelativeLayout) v.findViewById(R.id.layout_list);
													layout_list3.setBackground(getResources().getDrawable(R.drawable.round_auto_select_layout));
												}else{
													RelativeLayout layout_list3 = (RelativeLayout) v.findViewById(R.id.layout_list);
													layout_list3.setBackground(getResources().getDrawable(R.drawable.round_default_layout));
												}
											}else{
												RelativeLayout layout_list3 = (RelativeLayout) v.findViewById(R.id.layout_list);
												layout_list3.setBackground(getResources().getDrawable(R.drawable.round_default_layout));
											}

						                }
						            }
						            List<IndicatorResultVO> tempResult = null;
									if(isHomeStay){
										tempResult = Entity.query(IndicatorResultVO.class).where("subCategoryId").eq(subCategoryVO.id).and("homeId")
												.eq(homestayInfoVO.id).and("result").isNotNull().executeMulti();
									}else{
										tempResult = Entity.query(IndicatorResultVO.class).where("subCategoryId").eq(subCategoryVO.id).and("homeId").eq(homestayInfoVO.id)
													.and("houseId").eq(houseInfoVO.id).and("result").isNotNull().executeMulti();	
									}
									if(tempResult!=null){
										if(subCategoryVO.indicators().size()==tempResult.size()){
											RelativeLayout layout_list = (RelativeLayout) subList.findViewById(R.id.layout_list);
										    layout_list.setBackground(getResources().getDrawable(R.drawable.round_auto_select_layout));
										}else{
											RelativeLayout layout_list = (RelativeLayout) subList.findViewById(R.id.layout_list);
										    layout_list.setBackground(getResources().getDrawable(R.drawable.round_select_layout));
										}
									}else{
										RelativeLayout layout_list = (RelativeLayout) subList.findViewById(R.id.layout_list);
									    layout_list.setBackground(getResources().getDrawable(R.drawable.round_select_layout));
									}   
									new InitForListViewFirstSelect(2,subCount).execute();
							
							View indList = listViewForThirdSelect.getChildAt(indCount);
							if(indList!=null){
								View v2;
							            for (int i=0; i<indCount; i++)
							            {
							                if (i!= indCount)
							                {
							                    v2 = listViewForThirdSelect.getChildAt(i); 
							                    RelativeLayout layout_list2 = (RelativeLayout) v2.findViewById(R.id.layout_list);
							                    layout_list2.setBackground(getResources().getDrawable(R.drawable.round_default_layout));

							                }
							            }

							    RelativeLayout layout_list2 = (RelativeLayout) indList.findViewById(R.id.layout_list);
								layout_list2.setBackground(getResources().getDrawable(R.drawable.round_select_layout));
							}
						}
					}else{
						subCount = 0;
						catCount++;
						if(catCount<homestayVO.categories().size()){
							categoryVO =  homestayVO.categories().get(catCount);
							subCategoryVO = categoryVO.subCategories().get(subCount);
							indicatorVO = subCategoryVO.indicators().get(indCount);
							View catList = listViewForFirstSelect.getChildAt(catCount);
							
							if(catList!=null){
								View v;
										int indSize = 0;
										
							            for (int i=0; i<catCount; i++)
							            {
							                if (i!= catCount)
							                {
							                    v = listViewForFirstSelect.getChildAt(i); 
							                    CategoryVO tempCategoryVO =  homestayVO.categories().get(i);
									            List<IndicatorResultVO> tempResult = null;
												if(isHomeStay){
													tempResult = Entity.query(IndicatorResultVO.class).where("categoryId").eq(tempCategoryVO.id).and("homeId")
															.eq(homestayInfoVO.id).and("result").isNotNull().executeMulti();
												}else{
													tempResult = Entity.query(IndicatorResultVO.class).where("categoryId").eq(tempCategoryVO.id).and("homeId").eq(homestayInfoVO.id)
																.and("houseId").eq(houseInfoVO.id).and("result").isNotNull().executeMulti();	
												}
												if(tempResult!=null){
													List<SubCategoryVO> subList = tempCategoryVO.subCategories();
													int tempIndSize = 0;
													for(SubCategoryVO sub : subList){
														tempIndSize += sub.indicators().size();
													}
													if(tempIndSize==tempResult.size()){
														RelativeLayout layout_list = (RelativeLayout) v.findViewById(R.id.layout_list);
													    layout_list.setBackground(getResources().getDrawable(R.drawable.round_auto_select_layout));
													}else{
														RelativeLayout layout_list = (RelativeLayout) v.findViewById(R.id.layout_list);
													    layout_list.setBackground(getResources().getDrawable(R.drawable.round_default_layout));
													}
												}else{
													RelativeLayout layout_list = (RelativeLayout) v.findViewById(R.id.layout_list);
												    layout_list.setBackground(getResources().getDrawable(R.drawable.round_default_layout));
												}

							                }
							            }
							            List<IndicatorResultVO> tempResult1 = null;
										if(isHomeStay){
											tempResult1 = Entity.query(IndicatorResultVO.class).where("categoryId").eq(categoryVO.id).and("homeId").eq(homestayInfoVO.id).executeMulti();
										}else{
											tempResult1 = Entity.query(IndicatorResultVO.class).where("categoryId").eq(categoryVO.id).and("homeId").eq(homestayInfoVO.id)
														.and("houseId").eq(houseInfoVO.id).executeMulti();	
										}
										if(tempResult1!=null){
											List<SubCategoryVO> subList = categoryVO.subCategories();
											for(SubCategoryVO sub : subList){
												indSize += sub.indicators().size();
											}
											if(indSize==tempResult1.size()){
												RelativeLayout layout_list = (RelativeLayout) catList.findViewById(R.id.layout_list);
											    layout_list.setBackground(getResources().getDrawable(R.drawable.round_auto_select_layout));
											}else{
												RelativeLayout layout_list = (RelativeLayout) catList.findViewById(R.id.layout_list);
											    layout_list.setBackground(getResources().getDrawable(R.drawable.round_select_layout));
											}
										}else{
											RelativeLayout layout_list = (RelativeLayout) catList.findViewById(R.id.layout_list);
										    layout_list.setBackground(getResources().getDrawable(R.drawable.round_select_layout));
										}        
								
								new InitForListViewFirstSelect(1,catCount).execute();
								
								
								View subList = listViewForSecondSelect.getChildAt(subCount);
								if(subList!=null){
									View v3;
								            for (int i=0; i<subCount; i++)
								            {
								                if (i!= subCount)
								                {
								                	v3 = listViewForSecondSelect.getChildAt(i); 
								                	  SubCategoryVO tempSubCategoryVO =  selCategoryVO.subCategories().get(i);
											            List<IndicatorResultVO> tempResult = null;
														if(isHomeStay){
															tempResult = Entity.query(IndicatorResultVO.class).where("subCategoryId").eq(tempSubCategoryVO.id).and("homeId")
																	.eq(homestayInfoVO.id).and("result").isNotNull().executeMulti();
														}else{
															tempResult = Entity.query(IndicatorResultVO.class).where("subCategoryId").eq(tempSubCategoryVO.id).and("homeId").eq(homestayInfoVO.id)
																		.and("houseId").eq(houseInfoVO.id).and("result").isNotNull().executeMulti();	
														}
														if(tempResult!=null){
															if(tempSubCategoryVO.indicators().size()==tempResult.size()){
																RelativeLayout layout_list3 = (RelativeLayout) v3.findViewById(R.id.layout_list);
																layout_list3.setBackground(getResources().getDrawable(R.drawable.round_auto_select_layout));
															}else{
																RelativeLayout layout_list3 = (RelativeLayout) v3.findViewById(R.id.layout_list);
																layout_list3.setBackground(getResources().getDrawable(R.drawable.round_default_layout));
															}
														}else{
															RelativeLayout layout_list3 = (RelativeLayout) v3.findViewById(R.id.layout_list);
															layout_list3.setBackground(getResources().getDrawable(R.drawable.round_default_layout));
														}

								                }
								            }

								    RelativeLayout layout_list3 = (RelativeLayout) subList.findViewById(R.id.layout_list);
								    layout_list3.setBackground(getResources().getDrawable(R.drawable.round_select_layout));
									new InitForListViewFirstSelect(2,subCount).execute();
								}
								View indList = listViewForThirdSelect.getChildAt(indCount);
								if(indList!=null){
									View v2;
								            for (int i=0; i<indCount; i++)
								            {
								                if (i!= indCount)
								                {
								                    v2 = listViewForThirdSelect.getChildAt(i); 
										            List<IndicatorResultVO> tempResult = null;
													if(isHomeStay){
														tempResult = Entity.query(IndicatorResultVO.class).where("indicatorId").eq(indicatorVO.id).and("homeId")
																.eq(homestayInfoVO.id).and("result").isNotNull().executeMulti();
													}else{
														tempResult = Entity.query(IndicatorResultVO.class).where("indicatorId").eq(indicatorVO.id).and("homeId").eq(homestayInfoVO.id)
																	.and("houseId").eq(houseInfoVO.id).and("result").isNotNull().executeMulti();	
													}
													if(tempResult!=null && !tempResult.isEmpty()){
														RelativeLayout layout_list2 = (RelativeLayout) v2.findViewById(R.id.layout_list);
														layout_list2.setBackground(getResources().getDrawable(R.drawable.round_auto_select_layout));
													}else{
														RelativeLayout layout_list2 = (RelativeLayout) v2.findViewById(R.id.layout_list);
														layout_list2.setBackground(getResources().getDrawable(R.drawable.round_default_layout));
													}

								                }
								            }

								    RelativeLayout layout_list2 = (RelativeLayout) indList.findViewById(R.id.layout_list);
									layout_list2.setBackground(getResources().getDrawable(R.drawable.round_select_layout));
								}
							}
						}
					}
				}
				if(catCount<homestayVO.categories().size()){
					catCountStr.setText(categoryVO.no);
					catRatio.setText((catCount+1)+"/"+homestayVO.categories().size());
					catTitle.setText(categoryVO.title);
					
					subCountStr.setText((subCount+1)+"");
					subTotal.setText("/"+categoryVO.subCategories().size());
					subName.setText(subCategoryVO.title);
					
					indCountStr.setText((indCount+1)+"");
					indTotal.setText("/"+subCategoryVO.indicators().size());
					indName.setText(indicatorVO.title);
				}

				
				
				checkBt();
			}else{
				if(isHomeStay){
				switchFragment(new AfterSaveEvaluateFormFragment(_sm,homestayVO,homestayInfoVO,null,isHomeStay));
				}else{
					switchFragment(new AfterSaveEvaluateFormFragment(_sm,homestayVO,homestayInfoVO,houseInfoVO,isHomeStay));	
				}
			}
			listImgPath = inResultVO.getImgList(inResultVO);
			initPopup();
			
			
		}	
	}
	public void updateSelForms(){
		if(catCount<homestayVO.categories().size()){
			categoryVO =  homestayVO.categories().get(catCount);
			subCategoryVO = categoryVO.subCategories().get(subCount);
			indicatorVO = subCategoryVO.indicators().get(indCount);
			if(catCount<homestayVO.categories().size()){
				catCountStr.setText(categoryVO.no);
				catRatio.setText((catCount+1)+"/"+homestayVO.categories().size());
				catTitle.setText(categoryVO.title);
				
				subCountStr.setText((subCount+1)+"");
				subTotal.setText("/"+categoryVO.subCategories().size());
				subName.setText(subCategoryVO.title);
				
				indCountStr.setText((indCount+1)+"");
				indTotal.setText("/"+subCategoryVO.indicators().size());
				indName.setText(indicatorVO.title);
				checkBt();
			}
			listImgPath = inResultVO.getImgList(inResultVO);
			initPopup();
			
		}	
	}
	public void updateSelSubForms(){
		if(catCount<homestayVO.categories().size()){
			indCount = 0;
			categoryVO =  homestayVO.categories().get(catCount);
			subCategoryVO = categoryVO.subCategories().get(subCount);
			indicatorVO = subCategoryVO.indicators().get(indCount);
			if(catCount<homestayVO.categories().size()){
				catCountStr.setText(categoryVO.no);
				catRatio.setText((catCount+1)+"/"+homestayVO.categories().size());
				catTitle.setText(categoryVO.title);
				
				subCountStr.setText((subCount+1)+"");
				subTotal.setText("/"+categoryVO.subCategories().size());
				subName.setText(subCategoryVO.title);
				
				indCountStr.setText((indCount+1)+"");
				indTotal.setText("/"+subCategoryVO.indicators().size());
				indName.setText(indicatorVO.title);
				checkBt();
			}
			listImgPath = inResultVO.getImgList(inResultVO);
			initPopup();
			
		}	
	}
	public void updatePrevForms(){
		if(catCount>=0 && subCount>=0){
			
			if(indCount >0){
				indCount--;
				categoryVO =  homestayVO.categories().get(catCount);
				subCategoryVO = categoryVO.subCategories().get(subCount);
				indicatorVO = subCategoryVO.indicators().get(indCount);
				View indList = listViewForThirdSelect.getChildAt(indCount);
				if(indList!=null){
					View v;
				            for (int i=0; i<indCount; i++)
				            {
				                if (i!= indCount)
				                {
				                    v = listViewForThirdSelect.getChildAt(i); 
				                    RelativeLayout layout_list = (RelativeLayout) v.findViewById(R.id.layout_list);
				                    layout_list.setBackground(getResources().getDrawable(R.drawable.round_default_layout));

				                }
				            }

				    RelativeLayout layout_list = (RelativeLayout) indList.findViewById(R.id.layout_list);
					layout_list.setBackground(getResources().getDrawable(R.drawable.round_select_layout));
				}
			}else{
				if(subCount >0){
					categoryVO =  homestayVO.categories().get(catCount);
					subCount--;
					subCategoryVO = categoryVO.subCategories().get(subCount);
					indCount = subCategoryVO.indicators().size()-1;
					indicatorVO = subCategoryVO.indicators().get(indCount);
					View subList = listViewForSecondSelect.getChildAt(subCount);
					if(subList!=null){
						View v;
					            for (int i=0; i<subCount; i++)
					            {
					                if (i!= subCount)
					                {
					                    v = listViewForSecondSelect.getChildAt(i); 
					                    RelativeLayout layout_list = (RelativeLayout) v.findViewById(R.id.layout_list);
					                    layout_list.setBackground(getResources().getDrawable(R.drawable.round_default_layout));

					                }
					            }

					    RelativeLayout layout_list = (RelativeLayout) subList.findViewById(R.id.layout_list);
						layout_list.setBackground(getResources().getDrawable(R.drawable.round_select_layout));
						new InitForListViewFirstSelect(2,subCount).execute();
						
						View indList = listViewForThirdSelect.getChildAt(indCount);
						if(indList!=null){
							View v2;
						            for (int i=0; i<indCount; i++)
						            {
						                if (i!= indCount)
						                {
						                    v2 = listViewForThirdSelect.getChildAt(i); 
						                    RelativeLayout layout_list2 = (RelativeLayout) v2.findViewById(R.id.layout_list);
						                    layout_list2.setBackground(getResources().getDrawable(R.drawable.round_default_layout));

						                }
						            }

						    RelativeLayout layout_list2 = (RelativeLayout) indList.findViewById(R.id.layout_list);
							layout_list2.setBackground(getResources().getDrawable(R.drawable.round_select_layout));
						}
					}
				}else{
					if(catCount>0){
						catCount--;
						categoryVO =  homestayVO.categories().get(catCount);
						subCount = categoryVO.subCategories().size()-1;
						subCategoryVO = categoryVO.subCategories().get(subCount);
						indCount = subCategoryVO.indicators().size()-1;
						indicatorVO = subCategoryVO.indicators().get(indCount);	
						View catList = listViewForFirstSelect.getChildAt(catCount);
						
						if(catList!=null){
							View v;
						            for (int i=0; i<catCount; i++)
						            {
						                if (i!= catCount)
						                {
						                    v = listViewForFirstSelect.getChildAt(i); 
						                    RelativeLayout layout_list = (RelativeLayout) v.findViewById(R.id.layout_list);
						                    layout_list.setBackground(getResources().getDrawable(R.drawable.round_default_layout));

						                }
						            }

						    RelativeLayout layout_list = (RelativeLayout) catList.findViewById(R.id.layout_list);
							layout_list.setBackground(getResources().getDrawable(R.drawable.round_select_layout));
							
							new InitForListViewFirstSelect(1,catCount).execute();
							
							
							View subList = listViewForSecondSelect.getChildAt(subCount);
							if(subList!=null){
								View v3;
							            for (int i=0; i<subCount; i++)
							            {
							                if (i!= subCount)
							                {
							                	v3 = listViewForSecondSelect.getChildAt(i); 
							                    RelativeLayout layout_list3 = (RelativeLayout) v3.findViewById(R.id.layout_list);
							                    layout_list3.setBackground(getResources().getDrawable(R.drawable.round_default_layout));

							                }
							            }

							    RelativeLayout layout_list3 = (RelativeLayout) subList.findViewById(R.id.layout_list);
							    layout_list3.setBackground(getResources().getDrawable(R.drawable.round_select_layout));
								new InitForListViewFirstSelect(2,subCount).execute();
							}
							View indList = listViewForThirdSelect.getChildAt(indCount);
							if(indList!=null){
								View v2;
							            for (int i=0; i<indCount; i++)
							            {
							                if (i!= indCount)
							                {
							                    v2 = listViewForThirdSelect.getChildAt(i); 
							                    RelativeLayout layout_list2 = (RelativeLayout) v2.findViewById(R.id.layout_list);
							                    layout_list2.setBackground(getResources().getDrawable(R.drawable.round_default_layout));

							                }
							            }

							    RelativeLayout layout_list2 = (RelativeLayout) indList.findViewById(R.id.layout_list);
								layout_list2.setBackground(getResources().getDrawable(R.drawable.round_select_layout));
							}
						}
					}
				}
			}
				catCountStr.setText(categoryVO.no);
				catRatio.setText((catCount+1)+"/"+homestayVO.categories().size());
				catTitle.setText(categoryVO.title);
				
				subCountStr.setText((subCount+1)+"");
				subTotal.setText("/"+categoryVO.subCategories().size());
				subName.setText(subCategoryVO.title);
				
				indCountStr.setText((indCount+1)+"");
				indTotal.setText("/"+subCategoryVO.indicators().size());
				indName.setText(indicatorVO.title);
				
				checkBt();
				listImgPath = inResultVO.getImgList(inResultVO);
				initPopup();
				
		}
		
	}
	public void saveForms(String result){
		
		if(isHomeStay){
		inResultVO = Entity.query(IndicatorResultVO.class).where("indicatorId").eq(indicatorVO.id+"").and("homeId").eq(homestayInfoVO.id).execute();
		}else{
		inResultVO = Entity.query(IndicatorResultVO.class).where("indicatorId").eq(indicatorVO.id+"").and("homeId").eq(homestayInfoVO.id)
					.and("houseId").eq(houseInfoVO.id).execute();	
		}
		
		if(inResultVO==null){
			inResultVO = new IndicatorResultVO();
			inResultVO.id = inResultVO.getPk();
			inResultVO.indicatorId = indicatorVO.id+"";
			inResultVO.categoryId = categoryVO.id+"";
			inResultVO.subCategoryId = subCategoryVO.id+"";
			inResultVO.homeId = homestayInfoVO.id+"";
		}
		inResultVO.result = result;
		
		if(isHomeStay){
			inResultVO.houseId ="0";
		}else{
			inResultVO.houseId = houseInfoVO.id+"";
		}
		inResultVO.comment_text = addingComment.getText().toString();
		inResultVO.save();
	}
	
	public class InitForListViewFirstSelect extends AsyncTask<String, Void, List<ItemType>> implements OnCancelListener{
		ProgressHUD mProgressHUD;
		private int selCase;
		private int id;
		public InitForListViewFirstSelect(int selCase){
			this.selCase=selCase;
		}
		public InitForListViewFirstSelect(int selCase,int id){
			this.selCase=selCase;
			this.id=id;
		}
    	@Override
    	protected void onPreExecute() {
        	mProgressHUD = ProgressHUD.show(getActivity(),"Loading Content...", true,true,this);
    		super.onPreExecute();
    	}
		@Override
		protected List<ItemType> doInBackground(String... params) {
			// TODO Auto-generated method stub	
			List<ItemType> list = new ArrayList<ItemType>();
			switch (selCase) {
			case 0:
				
				List<CategoryVO> categories =  homestayVO.categories();
				if(categories!=null && categories.size()>0){
					for(CategoryVO cat : categories){
						ItemType item = new ItemType();
						item.id = cat.id;
						item.title = cat.title;
						item.type = 0;
						List<IndicatorResultVO> tempResult = null;
						if(isHomeStay){
							tempResult = Entity.query(IndicatorResultVO.class).where("categoryId").eq(cat.id).and("homeId")
									.eq(homestayInfoVO.id).and("result").isNotNull().executeMulti();
						}else{
							tempResult = Entity.query(IndicatorResultVO.class).where("categoryId").eq(cat.id).and("homeId").eq(homestayInfoVO.id)
										.and("houseId").eq(houseInfoVO.id).and("result").isNotNull().executeMulti();	
						}
						if(tempResult!=null){
							List<SubCategoryVO> subList = cat.subCategories();
							int indSize = 0;
							for(SubCategoryVO sub : subList){
								indSize += sub.indicators().size();
							}
							if(indSize==tempResult.size()){
								item.selected = true;
							}else{
								item.selected = false;
							}
						}else{
							item.selected = false;
						}
						list.add(item);
						
					}
				}
				break;
			case 1:
				selCategoryVO = homestayVO.categories().get(id);
				List<SubCategoryVO> subCategories = selCategoryVO.subCategories();
				if(subCategories!=null && subCategories.size()>0){
					for(SubCategoryVO cat : subCategories){
						ItemType item = new ItemType();
						item.id = cat.id;
						item.title = cat.title;
						item.type = 1;
						List<IndicatorResultVO> tempResult = null;
						if(isHomeStay){
							tempResult = Entity.query(IndicatorResultVO.class).where("subCategoryId").eq(cat.id).and("homeId")
									.eq(homestayInfoVO.id).and("result").isNotNull().executeMulti();
						}else{
							tempResult = Entity.query(IndicatorResultVO.class).where("subCategoryId").eq(cat.id).and("homeId").eq(homestayInfoVO.id)
										.and("houseId").eq(houseInfoVO.id).and("result").isNotNull().executeMulti();	
						}
						if(tempResult!=null){
							if(cat.indicators().size()==tempResult.size()){
								item.selected = true;
							}else{
								item.selected = false;
							}
						}else{
							item.selected = false;
						}
						list.add(item);
					}
				}
				break;
			case 2:
				selSubCategoryVO = selCategoryVO.subCategories().get(id);
				List<IndicatorVO> inList = selSubCategoryVO.indicators();
				if(inList!=null && inList.size()>0){
					for(IndicatorVO cat : inList){
						ItemType item = new ItemType();
						item.id = cat.id;
						item.title = cat.title;
						item.type = 2;
						IndicatorResultVO tempResult = null;
						if(isHomeStay){
							tempResult = Entity.query(IndicatorResultVO.class).where("indicatorId").eq(cat.id).and("homeId")
									.eq(homestayInfoVO.id).and("result").isNotNull().execute();
						}else{
							tempResult = Entity.query(IndicatorResultVO.class).where("indicatorId").eq(cat.id).and("homeId").eq(homestayInfoVO.id)
										.and("houseId").eq(houseInfoVO.id).and("result").isNotNull().execute();	
						}
						if(tempResult!=null && (tempResult.result!=null && !tempResult.result.equals(""))){
							item.selected=true;
						}else{
							item.selected=false;
						}
						
						list.add(item);
					}
				}
				break;
			default:
				break;
			}

			return list;
		}
		
		@Override
		protected void onPostExecute(List<ItemType> result) {
			super.onPostExecute(result);
			if (result != null) {
				lv1adapter = new ListViewFirstInitMenuAdapter(result);
				switch (selCase) {
				case 0:
					listViewForFirstSelect.setAdapter(lv1adapter);
					noOfListColumn1.setText("("+String.valueOf(result.size())+")");
					break;
				case 1:
					listViewForSecondSelect.setAdapter(lv1adapter);
					List<ItemType> tempList = new ArrayList<ItemType>();
					listViewForThirdSelect.setAdapter(new ListViewFirstInitMenuAdapter(tempList));
					noOfListColumn2.setText("("+String.valueOf(result.size())+")");
					break;
				case 2:
					listViewForThirdSelect.setAdapter(lv1adapter);
					noOfListColumn3.setText("("+String.valueOf(result.size())+")");
					break;
				default:
					break;
				}
				
				mProgressHUD.dismiss();
			} else {
				mProgressHUD.dismiss();
			}
			
		}
		@Override
		public void onCancel(DialogInterface dialog) {
			// TODO Auto-generated method stub
			mProgressHUD.dismiss();
		}


	}
	public class ListViewFirstInitMenuAdapter extends BaseAdapter {
		private List<ItemType> _listSize;
		private ViewHolder viewForFirst;

		public ListViewFirstInitMenuAdapter(List<ItemType> list) {
			this._listSize = list;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return _listSize.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return _listSize.get(position);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup arg2) {
			// TODO Auto-generated method stub
			if (convertView == null) {
				viewForFirst = new ViewHolder();
				LayoutInflater inflater = (LayoutInflater) getActivity().getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = inflater.inflate(R.layout.each_list_evaluate,null);
				viewForFirst.content = (TextView) convertView.findViewById(R.id.content);	
				viewForFirst.layout_list = (RelativeLayout) convertView.findViewById(R.id.layout_list);
				
			}else{
				convertView.setTag(viewForFirst);	
			}
				ItemType item = _listSize.get(position);
				viewForFirst.content.setText(item.title);
				if((catCount==0 && subCount==0 ) && indCount==0){
				if(position==0){
				viewForFirst.layout_list.setBackground(getResources().getDrawable(R.drawable.round_select_layout));	
				}
				}else{
					if(subCount==0 && indCount==0){
						if(position==0){
							viewForFirst.layout_list.setBackground(getResources().getDrawable(R.drawable.round_select_layout));	
						}
					}else if(indCount==0){
						if(position==0){
							viewForFirst.layout_list.setBackground(getResources().getDrawable(R.drawable.round_select_layout));	
						}
					}else{
						viewForFirst.layout_list.setBackground(getResources().getDrawable(R.drawable.round_default_layout));
					}
				}
				switch (item.type) {
				case 0:
					if(item.selected){
						viewForFirst.layout_list.setBackground(getResources().getDrawable(R.drawable.round_auto_select_layout));
					}
					break;
				case 1:
					if(item.selected){
						viewForFirst.layout_list.setBackground(getResources().getDrawable(R.drawable.round_auto_select_layout));
					}
					break;
				case 2:
					if(item.selected){
					viewForFirst.layout_list.setBackground(getResources().getDrawable(R.drawable.round_auto_select_layout));
					}
					break;
				default:
					break;
				}
//				if(mSelectedItem==position){
//					viewForFirst.layout_list.setBackground(getResources().getDrawable(R.drawable.round_select_layout));
//				}else{
//					
//				}
								
			return convertView;
		}

	}
	public class ViewHolder {
		private TextView content;
		private RelativeLayout layout_list;
	}
	public class ItemType {
		private int id;
		private String title;
		private int type;
		private boolean selected;
		
	}
	public class InitAndLoadData extends AsyncTask<String, Void, List<String>> implements OnCancelListener{
		ProgressHUD mProgressHUD;
		
    	@Override
    	protected void onPreExecute() {
        	mProgressHUD = ProgressHUD.show(getActivity(),"Loading Content...", true,true,this);
    		super.onPreExecute();
    	}
		@Override
		protected List<String> doInBackground(String... params) {
			// TODO Auto-generated method stub	
			
			
			
			return null;
		}
		
		@Override
		protected void onPostExecute(List<String> result) {
			super.onPostExecute(result);
			if (result != null) {
				
				mProgressHUD.dismiss();
			} else {
				mProgressHUD.dismiss();
			}
			
		}
		@Override
		public void onCancel(DialogInterface dialog) {
			// TODO Auto-generated method stub
			mProgressHUD.dismiss();
		}


	}
	private void initPopup() {	
		animShow = AnimationUtils.loadAnimation(getActivity(), R.anim.popup_show);
		animHide = AnimationUtils.loadAnimation(getActivity(), R.anim.popup_hide);		
		// click this button to hide the popup
		hideButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				popup.startAnimation(animHide);
				// showButton.setEnabled(true);
				hideButton.setEnabled(true);
				popup.setVisibility(View.GONE);
			}
		});
	}
	private void initPopupForComment() {

		animShow = AnimationUtils.loadAnimation(getActivity(), R.anim.popup_show);
		animHide = AnimationUtils.loadAnimation(getActivity(), R.anim.popup_hide);
		
		// click this button to hide the popup
//		hideButton.setOnClickListener(new View.OnClickListener() {
//			public void onClick(View view) {
//				popup.startAnimation(animHide);
//				// showButton.setEnabled(true);
//				hideButton.setEnabled(true);
//				popup.setVisibility(View.GONE);
//			}
//		});
	}
	private void InitilizeGridLayout() {
		Resources r = getActivity().getResources();
		float padding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,AppConstant.GRID_PADDING, r.getDisplayMetrics());

		columnWidth = (int) ((utils.getScreenWidth() - ((AppConstant.NUM_OF_COLUMNS + 1) * padding)) / AppConstant.NUM_OF_COLUMNS);

		preImgGridView.setNumColumns(AppConstant.NUM_OF_COLUMNS);
		preImgGridView.setColumnWidth(columnWidth);
		preImgGridView.setStretchMode(GridView.NO_STRETCH);
		preImgGridView.setPadding((int) padding, (int) padding, (int) padding,(int) padding);
		preImgGridView.setHorizontalSpacing((int) padding);
		preImgGridView.setVerticalSpacing((int) padding);
	}
	public class AppConstant {

		// Number of columns of Grid View
		public static final int NUM_OF_COLUMNS = 3;

		// Gridview image padding
		public static final int GRID_PADDING = 10; // in dp

		// SD card image directory
		public static final String PHOTO_ALBUM = "NAT";

		// supported file formats
		public final List<String> FILE_EXTN = Arrays.asList("jpg", "jpeg","png");
	}
	/**
	 * ------------ Helper Methods ---------------------- 
	 * */

	/*
	 * Creating file uri to store image/video
	 */
	public Uri getOutputMediaFileUri(int type) {
		return Uri.fromFile(getOutputMediaFile(type));
	}

	/*
	 * returning image / video
	 */
	private static File getOutputMediaFile(int type) {

		// External sdcard location
		File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),IMAGE_DIRECTORY_NAME);
		
		// Create the storage directory if it does not exist
		if (!mediaStorageDir.exists()) {
			if (!mediaStorageDir.mkdirs()) {
				Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
						+ IMAGE_DIRECTORY_NAME + " directory");
				return null;
			}
		}

		// Create a media file name
		String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",Locale.getDefault()).format(new Date());
		File mediaFile;
		if (type == MEDIA_TYPE_IMAGE) {
			mediaFile = new File(mediaStorageDir.getPath() + File.separator+ "IMG_" + timeStamp + ".jpg");
		} else if (type == MEDIA_TYPE_VIDEO) {
			mediaFile = new File(mediaStorageDir.getPath() + File.separator+ "VID_" + timeStamp + ".mp4");
		} else {
			return null;
		}

		return mediaFile;
	}
	private void captureImage() {
		Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");

		fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
		intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

		// start the image capture Intent
		startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
	}
	/*
	 * Here we store the file url as it will be null after returning from camera
	 * app
	 */
	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);

		// save file url in bundle as it will be null on scren orientation
		// changes
		outState.putParcelable("file_uri", fileUri);
	}
	/**
	 * Receiving activity result method will be called after closing the camera
	 * */
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// if the result is capturing Image
		if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
			if (resultCode == getActivity().RESULT_OK) {				
				showCatureImageInGridView();
			} else if (resultCode == getActivity().RESULT_CANCELED) {
				// user cancelled Image capture
//				Toast.makeText(getActivity(),"User cancelled image capture", Toast.LENGTH_SHORT).show();
			} else {
				// failed to capture image
//				Toast.makeText(getActivity(),"Sorry! Failed to capture image", Toast.LENGTH_SHORT).show();
			}
		}
		if (requestCode == EX_FILE_PICKER_RESULT) {
			if (data != null) {
				ExFilePickerParcelObject object = (ExFilePickerParcelObject) data.getParcelableExtra(ExFilePickerParcelObject.class.getCanonicalName());
				if (object.count > 0) {
					StringBuffer buffer = new StringBuffer();
					for (int i = 0; i < object.count; i++) {
						buffer.append(object.names.get(i));
						if (i < object.count - 1) buffer.append(", ");
					}					
					String[] nameSplit = buffer.toString().split(", ");
					if(object.count>1){						
						for(int j=0;j<nameSplit.length;j++){
							listImgPath.add(object.path+nameSplit[j]);
						}						
						GridViewImageForDeleteAdapter adapter = new GridViewImageForDeleteAdapter(getActivity(), listImgPath, columnWidth, inResultVO);
						adapter.notifyDataSetChanged();
						preImgGridView.setAdapter(adapter);
//						floatingActionButton.setVisibility(View.VISIBLE);
						
					}else{
						
						listImgPath.add(object.path+buffer.toString());
						GridViewImageForDeleteAdapter adapter = new GridViewImageForDeleteAdapter(getActivity(), listImgPath, columnWidth, inResultVO);
						adapter.notifyDataSetChanged();
						preImgGridView.setAdapter(adapter);
//						floatingActionButton.setVisibility(View.VISIBLE);
					}
				}
			}
		}
	}
	public void showCatureImageInGridView(){
		InputStream iStream;
		try {
			
			Bitmap bitmap = decodeSampledBitmapFromFile(fileUri.getPath(), 460, 292);
			ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();  
			bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
			byte[] byteArray = byteArrayOutputStream .toByteArray();
			listImgPath.add(Base64.encodeToString(byteArray, Base64.DEFAULT));
			GridViewImageForDeleteAdapter adapter = new GridViewImageForDeleteAdapter(getActivity(), listImgPath, columnWidth, inResultVO);
			adapter.notifyDataSetChanged();
			preImgGridView.setAdapter(adapter);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	public byte[] getBytes(InputStream inputStream) throws IOException {
	      ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
	      int bufferSize = 1024;
	      byte[] buffer = new byte[bufferSize];

	      int len = 0;
	      while ((len = inputStream.read(buffer)) != -1) {
	        byteBuffer.write(buffer, 0, len);
	      }
	      return byteBuffer.toByteArray();
	 }
	public void checkBt(){
		if("T".equals(indicatorVO.isInterview)){
			interView.setImageResource(R.drawable.interview_evaluate_sel);
		}else{
			interView.setImageResource(R.drawable.interview_evaluate);
		}
		if("T".equals(indicatorVO.isDocument)){
			documentView.setImageResource(R.drawable.document_evaluate_sel);
		}else{
			documentView.setImageResource(R.drawable.document_evaluate);
		}
		if("T".equals(indicatorVO.isCamera)){
			viewImage.setImageResource(R.drawable.image_evaluate_sel);
		}else{
			viewImage.setImageResource(R.drawable.image_evaluate); 
		}
		 
			if(isHomeStay){
			inResultVO = Entity.query(IndicatorResultVO.class).where("indicatorId").eq(indicatorVO.id).and("homeId").eq(homestayInfoVO.id).execute();
			}else{
			inResultVO = Entity.query(IndicatorResultVO.class).where("indicatorId").eq(indicatorVO.id).and("homeId").eq(homestayInfoVO.id)
						.and("houseId").eq(houseInfoVO.id).execute();	
			}
			if(inResultVO!=null){
				if(inResultVO.result!=null && !"".equals(inResultVO.result)){
					yesBt.setAlpha(!"0".equals(inResultVO.result)?0.5F:1F);
					noBt.setAlpha(!"1".equals(inResultVO.result)?0.5F:1F);
					noneBt.setAlpha(!"2".equals(inResultVO.result)?0.5F:1F);
				}
				View catList = listViewForFirstSelect.getChildAt(catCount);
				if(catList!=null){
				
				 
					List<IndicatorVO> indVoList = subCategoryVO.indicators();
					if(indVoList!=null && indVoList.size()>0){
						for(int i=0;i<indVoList.size();i++){
							IndicatorVO vo = indVoList.get(i);
							IndicatorResultVO tempResult = null;
							if(isHomeStay){
								tempResult = Entity.query(IndicatorResultVO.class).where("indicatorId").eq(vo.id).and("homeId")
										.eq(homestayInfoVO.id).and("result").isNotNull().execute();
								}else{
								tempResult = Entity.query(IndicatorResultVO.class).where("indicatorId").eq(vo.id).and("homeId").eq(homestayInfoVO.id)
											.and("houseId").eq(houseInfoVO.id).and("result").isNotNull().execute();	
								}
							if(tempResult!=null){
								View indList = listViewForThirdSelect.getChildAt(i);
								if(indList!=null){
								    RelativeLayout layout_list2 = (RelativeLayout) indList.findViewById(R.id.layout_list);
									layout_list2.setBackground(getResources().getDrawable(R.drawable.round_auto_select_layout));
								}
							}
						}
					}
				}
			}else{
				yesBt.setAlpha(1F);
				noBt.setAlpha(1F);
				noneBt.setAlpha(1F);
				inResultVO = new IndicatorResultVO();
				inResultVO.id = inResultVO.getPk();
				inResultVO.indicatorId = indicatorVO.id+"";
				inResultVO.homeId = homestayInfoVO.id+"";
			}
			
			
	}
	public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight) 
	{ // BEST QUALITY MATCH
	    
	    //First decode with inJustDecodeBounds=true to check dimensions
	    final BitmapFactory.Options options = new BitmapFactory.Options();
	    options.inJustDecodeBounds = true;
	    BitmapFactory.decodeFile(path, options);

	    // Calculate inSampleSize, Raw height and width of image
	    final int height = options.outHeight;
	    final int width = options.outWidth;
	    options.inPreferredConfig = Bitmap.Config.RGB_565;
	    int inSampleSize = 1;

	    if (height > reqHeight) 
	    {
	        inSampleSize = Math.round((float)height / (float)reqHeight);
	    }
	    int expectedWidth = width / inSampleSize;

	    if (expectedWidth > reqWidth) 
	    {
	        //if(Math.round((float)width / (float)reqWidth) > inSampleSize) // If bigger SampSize..
	        inSampleSize = Math.round((float)width / (float)reqWidth);
	    }

	    options.inSampleSize = inSampleSize;

	    // Decode bitmap with inSampleSize set
	    options.inJustDecodeBounds = false;

	    return BitmapFactory.decodeFile(path, options);
	}
	public class InitAndLoadImage extends AsyncTask<String, Void, List<String>> implements OnCancelListener{
		ProgressHUD mProgressHUD;
		
    	@Override
    	protected void onPreExecute() {
        	mProgressHUD = ProgressHUD.show(getActivity(),"Loading Content...", true,true,this);
    		super.onPreExecute();
    	}
		@Override
		protected List<String> doInBackground(String... params) {
			// TODO Auto-generated method stub			
			listImgPath = inResultVO.getImgList(inResultVO);
			return listImgPath;
		}
		
		@Override
		protected void onPostExecute(List<String> result) {
			super.onPostExecute(result);
			if (result != null) {				
				GridViewImageForDeleteAdapter adapter = new GridViewImageForDeleteAdapter(getActivity(), result, columnWidth, inResultVO);
				adapter.notifyDataSetChanged();
				preImgGridView.setAdapter(adapter);				
				isComment = false;
				popupForComment.startAnimation(animHide);
				// showButton.setEnabled(true);
				clickToHideComments.setEnabled(true);
				popupForComment.setVisibility(View.GONE);
				addingComment.setVisibility(View.GONE);
				popup.setVisibility(View.VISIBLE);
				popup.startAnimation(animShow);
				mProgressHUD.dismiss();
			} else {
				mProgressHUD.dismiss();
			}
			
		}
		@Override
		public void onCancel(DialogInterface dialog) {
			// TODO Auto-generated method stub
			mProgressHUD.dismiss();
		}


	}
	public class fakeLoading extends AsyncTask<String, Void, List<String>> implements OnCancelListener{
		ProgressHUD mProgressHUD;
		
    	@Override
    	protected void onPreExecute() {
        	mProgressHUD = ProgressHUD.show(getActivity(),"Loading Content...", true,true,this);
    		super.onPreExecute();
    	}
		@Override
		protected List<String> doInBackground(String... params) {
			// TODO Auto-generated method stub			
			try {
				Thread.sleep(800);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return listImgPath;
		}
		
		@Override
		protected void onPostExecute(List<String> result) {
			super.onPostExecute(result);
			if (result != null) {				
				
				mProgressHUD.dismiss();
			} else {
				mProgressHUD.dismiss();
			}
			
		}
		@Override
		public void onCancel(DialogInterface dialog) {
			// TODO Auto-generated method stub
			mProgressHUD.dismiss();
		}


	}
	public void uploadImageError(){
		if(builder1==null){
			builder1 = new AlertDialog.Builder(getActivity());
		}
		builder1.setMessage(getString(R.string.error_imgover));
        builder1.setCancelable(true);
        builder1.setPositiveButton(getString(R.string.homestay_sync_ok),new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
            }
        });
        AlertDialog alert11 = builder1.create();
        alert11.show();
	}
	public void onchangeCategory(AdapterView<?> arg0, View arg1, int arg2){
		selCatCount = arg2;
		View v;
	    int count = arg0.getChildCount();
	    		int indSize = 0;
	    		
	            for (int i=0; i<count; i++)
	            {
	                if (i!= arg2)
	                {
	                    v = arg0.getChildAt(i);
	                    CategoryVO tempCategoryVO =  homestayVO.categories().get(i);
			            List<IndicatorResultVO> tempResult = null;
						if(isHomeStay){
							tempResult = Entity.query(IndicatorResultVO.class).where("categoryId").eq(tempCategoryVO.id).and("homeId")
									.eq(homestayInfoVO.id).and("result").isNotNull().executeMulti();
						}else{
							tempResult = Entity.query(IndicatorResultVO.class).where("categoryId").eq(tempCategoryVO.id).and("homeId").eq(homestayInfoVO.id)
										.and("houseId").eq(houseInfoVO.id).and("result").isNotNull().executeMulti();	
						}
						if(tempResult!=null){
							List<SubCategoryVO> subList = tempCategoryVO.subCategories();
							int tempIndSize = 0;
							for(SubCategoryVO sub : subList){
								tempIndSize += sub.indicators().size();
							}
							if(tempIndSize==tempResult.size()){
								RelativeLayout layout_list = (RelativeLayout) v.findViewById(R.id.layout_list);
							    layout_list.setBackground(getResources().getDrawable(R.drawable.round_auto_select_layout));
							}else{
								RelativeLayout layout_list = (RelativeLayout) v.findViewById(R.id.layout_list);
							    layout_list.setBackground(getResources().getDrawable(R.drawable.round_default_layout));
							}
						}else{
							RelativeLayout layout_list = (RelativeLayout) v.findViewById(R.id.layout_list);
						    layout_list.setBackground(getResources().getDrawable(R.drawable.round_default_layout));
						}

	                } 
	            }
	            selCategoryVO =  homestayVO.categories().get(selCatCount);
	            List<IndicatorResultVO> tempResult = null;
				if(isHomeStay){ 
					tempResult = Entity.query(IndicatorResultVO.class).where("categoryId").eq(selCategoryVO.id).and("homeId").eq(homestayInfoVO.id).and("result").isNotNull().executeMulti();
				}else{
					tempResult = Entity.query(IndicatorResultVO.class).where("categoryId").eq(selCategoryVO.id).and("homeId").eq(homestayInfoVO.id)
								.and("houseId").eq(houseInfoVO.id).and("result").isNotNull().executeMulti();	
				}  
				if(tempResult!=null){
					List<SubCategoryVO> subList = selCategoryVO.subCategories();
					for(SubCategoryVO sub : subList){
						indSize += sub.indicators().size();
					}
					
					if(indSize==tempResult.size()){
						 RelativeLayout layout_list = (RelativeLayout) arg1.findViewById(R.id.layout_list);
						layout_list.setBackground(getResources().getDrawable(R.drawable.round_auto_select_layout));
					}else{
						 RelativeLayout layout_list = (RelativeLayout) arg1.findViewById(R.id.layout_list);
						layout_list.setBackground(getResources().getDrawable(R.drawable.round_select_layout));
					}
				}else{
					 RelativeLayout layout_list = (RelativeLayout) arg1.findViewById(R.id.layout_list);
					layout_list.setBackground(getResources().getDrawable(R.drawable.round_select_layout));
				}
	}
	public void onchangeSubCategory(AdapterView<?> arg0, View arg1, int arg2){
		// TODO Auto-generated method stub		
		catCount = selCatCount;
		selSubCount = arg2;
		subCount = arg2;
		View v;
	    int count = arg0.getChildCount();
	            for (int i=0; i<count; i++)
	            {
	                if (i!= arg2)
	                {
	                    v = arg0.getChildAt(i); 
	                    SubCategoryVO tempSubCategoryVO =  selCategoryVO.subCategories().get(i);
			            List<IndicatorResultVO> tempResult = null;
						if(isHomeStay){
							tempResult = Entity.query(IndicatorResultVO.class).where("subCategoryId").eq(tempSubCategoryVO.id).and("homeId").eq(homestayInfoVO.id)
									.and("result").isNotNull().executeMulti();
						}else{
							tempResult = Entity.query(IndicatorResultVO.class).where("subCategoryId").eq(tempSubCategoryVO.id).and("homeId").eq(homestayInfoVO.id)
										.and("houseId").eq(houseInfoVO.id).and("result").isNotNull().executeMulti();	
						}
						if(tempResult!=null){
							if(tempSubCategoryVO.indicators().size()==tempResult.size()){
								RelativeLayout layout_list = (RelativeLayout) v.findViewById(R.id.layout_list);
							    layout_list.setBackground(getResources().getDrawable(R.drawable.round_auto_select_layout));
							}else{
								RelativeLayout layout_list = (RelativeLayout) v.findViewById(R.id.layout_list);
							    layout_list.setBackground(getResources().getDrawable(R.drawable.round_default_layout));
							}
						}else{
							RelativeLayout layout_list = (RelativeLayout) v.findViewById(R.id.layout_list);
						    layout_list.setBackground(getResources().getDrawable(R.drawable.round_default_layout));
						}
	                }
	            }
	           
	            selSubCategoryVO =  selCategoryVO.subCategories().get(selSubCount);
	            List<IndicatorResultVO> tempResult = null;
				if(isHomeStay){
					tempResult = Entity.query(IndicatorResultVO.class).where("subCategoryId").eq(selSubCategoryVO.id).and("homeId")
							.eq(homestayInfoVO.id).and("result").isNotNull().executeMulti();
				}else{
					tempResult = Entity.query(IndicatorResultVO.class).where("subCategoryId").eq(selSubCategoryVO.id).and("homeId").eq(homestayInfoVO.id)
								.and("houseId").eq(houseInfoVO.id).and("result").isNotNull().executeMulti();	
				}
				if(tempResult!=null){
					if(selSubCategoryVO.indicators().size()==tempResult.size()){
						RelativeLayout layout_list = (RelativeLayout) arg1.findViewById(R.id.layout_list);
					    layout_list.setBackground(getResources().getDrawable(R.drawable.round_auto_select_layout));
					}else{
						RelativeLayout layout_list = (RelativeLayout) arg1.findViewById(R.id.layout_list);
					    layout_list.setBackground(getResources().getDrawable(R.drawable.round_select_layout));
					}
				}else{
					RelativeLayout layout_list = (RelativeLayout) arg1.findViewById(R.id.layout_list);
				    layout_list.setBackground(getResources().getDrawable(R.drawable.round_select_layout));
				}        
		
		new InitForListViewFirstSelect(2,arg2).execute();
		updateSelSubForms();
	}
	public void queryTempResult(){
		
	}
}
